// pages/edu/index/course/comment-reply/index.js
import {Comment} from '../../../model/comment.js'
const commentModel=new Comment()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    comment_id:0,
    comment_info:{},
    commentForm:false,
    commentContent:'',
    commentReplyIndex:0,
    loading:true,  //上拉加载
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.comment_id){
      this.setData({comment_id:options.comment_id})
    }
    this._getCommentInfo()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this._getCommentReply()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //-------------------------------

  /**
   * 评论信息包含回复
   */
  _getCommentInfo(){
    let comment_id=this.data.comment_id
    commentModel.getInfo(comment_id,(res)=>{
      this.setData({
        comment_info:res
      })
    })
  },

  /**
   * 上拉加载
   */
  _getCommentReply(){
    let comment_id=this.data.comment_id
    let commentReplyIndex=this.data.commentReplyIndex
    commentReplyIndex++
    console.log(commentReplyIndex)
    if(!this.data.loading){
      return ;
    }
    commentModel.getCommentReply(comment_id,commentReplyIndex,(res)=>{
      if(res.length>0){
        let comment_info=this.data.comment_info
        comment_info.get_comment_reply=comment_info.get_comment_reply.concat(res)
        this.setData({
          comment_info,
          commentReplyIndex
        })
      }else{
        this.data.loading=false
      }
    })

  },

  /**
   * 评论窗口
   */
  onComment(){ 
    this.setData({
      commentForm:true
    }) 
  },

  /**
   * 评论窗口关闭
   */
  onCommmentClose(){
    this.setData({
      commentForm:false
    })
  },

  /**
   * 评论内容
   */
  onCommentOk(event){
    let value=event.detail.value
    this.setData({
      commentContent:value
    })
  },

  /**
   * 提交评论内容
   */
  onCommentSubmit(){
    let commentContent=this.data.commentContent
    if(commentContent){
      let data={
        comment_id:this.data.comment_id,
        content:commentContent
      }

      commentModel.setCommentReply(data,(res)=>{
        wx.lin.showMessage({
          type:'success',
          content:'评论成功'
        })

        this.onCommmentClose()
        this._getCommentInfo()
      },(res)=>{
        wx.lin.showMessage({
          type:'warning',
          content:res.msg
        })
      })

    }else{
      wx.lin.showMessage({
        type:'warning',
        content:'请输入评论内容~'
      })
    }
  },

})